
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

public class BootStrapDG extends Canvas implements Runnable, MouseListener {
    public static final float UPDATE_RATE = 60.0f;
    public static final float UPDATE_INTERVAL = Time.SECOND / UPDATE_RATE;
    public static final long IDLE_TIME = 1;
    GameStats gameStats;
    Thread thread;
    DoraemonObjectManager doraemonObjectManager;
    MedusaObjectManager medusaObjectManager;
    Maps maps = new Maps();
    UserInterface userInterface = new UserInterface(Global.GAMEWIDTH, Global.GAMEHEIGHT, "棋盘游戏", this);

    static int playerorenmeylist[][] = {
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
    };

    public BootStrapDG() {
        this.setBounds(0, 0, Global.GAMEWIDTH, Global.GAMEHEIGHT);
        doraemonObjectManager = new DoraemonObjectManager();
        medusaObjectManager = new MedusaObjectManager();
        gameStats = new GameStats();
        this.addMouseListener(this);

        for (int i = 0; i < playerorenmeylist.length; i++) {
            for (int j = 0; j < playerorenmeylist[0].length; j++) {
                if (playerorenmeylist[i][j] == 2) {
                    doraemonObjectManager.addPlayer(new AbstractBiology(i, j, Color.BLACK, "image/rpg3.png"));
                } else if (playerorenmeylist[i][j] == 3) {
                    medusaObjectManager.addPlayer(new AbstractBiology(i, j, Color.magenta, "image/rpgmove2.png"));
                }
            }
        }
        medusaObjectManager.enemyLinked.get(0).setImagePath("image/哆啦A梦/哆啦A梦动作.png");
        medusaObjectManager.enemyLinked.get(1).setImagePath("image/哆啦A梦/哆啦利钮.png");
        medusaObjectManager.enemyLinked.get(2).setImagePath("image/哆啦A梦/哆啦咪子.png");
        medusaObjectManager.enemyLinked.get(3).setImagePath("image/哆啦A梦/哆啦小子.png");
        medusaObjectManager.enemyLinked.get(4).setImagePath("image/哆啦A梦/哆啦尼可夫.png");
        medusaObjectManager.enemyLinked.get(5).setImagePath("image/哆啦A梦/哆啦杰.png");
        medusaObjectManager.enemyLinked.get(6).setImagePath("image/哆啦A梦/哆啦梅度三世.png");
        medusaObjectManager.enemyLinked.get(7).setImagePath("image/哆啦A梦/哆啦王.png");
        medusaObjectManager.enemyLinked.get(8).setImagePath("image/哆啦A梦/哆啦美.png");
        medusaObjectManager.enemyLinked.get(9).setImagePath("image/哆啦A梦/哆啦邦.png");
        doraemonObjectManager.abstractBiologyLinked.get(0).setMoveType(0);
        doraemonObjectManager.abstractBiologyLinked.get(1).setMoveType(1);
        doraemonObjectManager.abstractBiologyLinked.get(2).setMoveType(2);
        doraemonObjectManager.abstractBiologyLinked.get(3).setMoveType(3);
        doraemonObjectManager.abstractBiologyLinked.get(0).setImagePath("image/玩家/r3.png");
        doraemonObjectManager.abstractBiologyLinked.get(1).setImagePath("image/玩家/r2.png");
        doraemonObjectManager.abstractBiologyLinked.get(2).setImagePath("image/玩家/r1.png");
        doraemonObjectManager.abstractBiologyLinked.get(3).setImagePath("image/玩家/r3.png");
        start();
    }

    public void run() {
        long count = 0;
        float delta = 0;
        long lastTime = Time.get();
        while (true) {
            long now = Time.get();
            long elapsedTime = now - lastTime;
            lastTime = now;
            count += elapsedTime;
            boolean render = false;
            delta += (elapsedTime / UPDATE_INTERVAL);
            while (delta > 1) {
                this.tick();
                this.render();
                delta--;
                if (render) {
                } else {
                    render = true;
                }
            }

            if (render) {
            } else {
                try {
                    Thread.sleep(IDLE_TIME);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (count >= Time.SECOND) {
                count = 0;
            }

        }

    }

    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
    }

    public synchronized void stop() {
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // DrawAllDoraemonPhotoAndInfo drawAllDoraemonPhotoAndInfo = new DrawAllDoraemonPhotoAndInfo();
    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if (bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, Global.GAMEWIDTH, Global.GAMEHEIGHT);
        maps.render(g, doraemonObjectManager, medusaObjectManager, gameStats);
        medusaObjectManager.render(g, maps, doraemonObjectManager);
        doraemonObjectManager.render(g, maps, medusaObjectManager);
        // drawAllDoraemonPhotoAndInfo.render(g,doraemonObjectManager);

        g.dispose();
        bs.show();
    }

    private void tick() {
        doraemonObjectManager.tick();
        medusaObjectManager.tick();
        //drawAllDoraemonPhotoAndInfo.tick();
    }

    public static void main(String args[]) {
        new BootStrapDG();
    }

    public void mouseClicked(java.awt.event.MouseEvent e) {

    }

    public void mousePressed(java.awt.event.MouseEvent e) {
        if (gameStats.getCURRENTSTATS() == gameStats.PLAYERTURN) {

            doraemonObjectManager.playerMove(e, gameStats, doraemonObjectManager, medusaObjectManager, maps);
            if (e.getX() > 0 && e.getX() < Global.CELL_SIZE && e.getY() > 0 && e.getY() < Global.CELL_SIZE) {
                for (int i = 0; i < doraemonObjectManager.abstractBiologyLinked.size(); i++) {
                    doraemonObjectManager.abstractBiologyLinked.get(i).setSelected(false);
                    doraemonObjectManager.abstractBiologyLinked.get(i).setMove(false);
                    doraemonObjectManager.abstractBiologyLinked.get(i).setAttack(false);
                }
                doraemonObjectManager.abstractBiologyLinked.get((doraemonObjectManager.getcurrentplayer + 1) % doraemonObjectManager.abstractBiologyLinked.size()).setSelected(true);
            }
        } else {
            try {
                medusaObjectManager.enemyMove(maps, gameStats, e, doraemonObjectManager, medusaObjectManager, thread);
                // thread.sleep(1000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }

        }
    }

    public void mouseReleased(java.awt.event.MouseEvent e) {
    }

    public void mouseEntered(java.awt.event.MouseEvent e) {
    }

    public void mouseExited(java.awt.event.MouseEvent e) {
    }
}
